package cn.ezios.huo.system.dao.service.impl;

import cn.ezios.huo.system.dao.dto.SysRoleDTO;
import cn.ezios.huo.system.dao.service.SysRoleMenuService;
import cn.ezios.huo.system.dao.vo.SysRoleVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ezios.huo.system.dao.domain.SysRole;
import cn.ezios.huo.system.dao.service.SysRoleService;
import cn.ezios.huo.system.dao.mapper.SysRoleMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author 王坚
* @description 针对表【sys_role(系统角色)】的数据库操作Service实现
* @createDate 2024-07-17 09:49:16
*/
@Service
@AllArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService{

    private final SysRoleMenuService sysRoleMenuService;

    /**
     * 分页查询
     *
     * @param current
     * @param size
     * @param dto
     * @return
     */
    @Override
    public IPage<SysRoleVO> queryPage(Integer current, Integer size, SysRoleDTO dto) {
        Page<SysRole> page = Page.of(current, size);
        return lambdaQuery()
                .like(dto.getRoleName() != null && !dto.getRoleName().isBlank(),SysRole::getRoleName, dto.getRoleName())
                .page(page)
                .convert(origin->{
                    SysRoleVO vo = new SysRoleVO();
                    BeanUtils.copyProperties(origin, vo);
                    return vo;
                });
    }

    /**
     * 新增角色
     *
     * @param sysRoleDTO
     * @return
     */
    @Override
    public boolean createRoleWithMenuIds(SysRoleDTO sysRoleDTO) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleDTO, sysRole);
        if (save(sysRole)) {
            sysRoleMenuService.createRoleMenu(sysRole.getRoleId(), sysRoleDTO.getMenuIds());
            return true;
        }
        return false;
    }

    /**
     * 修改角色
     *
     * @param sysRoleDTO
     * @return
     */
    @Override
    public boolean updateRoleWithMenuIds(SysRoleDTO sysRoleDTO) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleDTO, sysRole);
        if (updateById(sysRole)) {
            sysRoleMenuService.deleteRoleMenu(sysRoleDTO.getRoleId());
            sysRoleMenuService.createRoleMenu(sysRoleDTO.getRoleId(), sysRoleDTO.getMenuIds());
            return true;
        }
        return false;
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteRoleWithMenuIds(String id) {
        if (removeById(id)) {
            sysRoleMenuService.deleteRoleMenu(id);
            return true;
        }
        return false;
    }

    /**
     * 根据id查询角色信息
     *
     * @param id
     * @return
     */
    @Override
    public SysRoleVO getRoleById(String id) {
        SysRoleVO vo = new SysRoleVO();
        SysRole sysRole = getById(id);
        if (sysRole != null) {
            BeanUtils.copyProperties(sysRole, vo);
            vo.setMenuIds(sysRoleMenuService.getRoleMenuIds(id));
            return vo;
        }
        return null;
    }

    /**
     * 根据id查询角色信息，叶子菜单
     *
     * @param id
     * @return
     */
    @Override
    public SysRoleVO getRoleWithLeafMenuIds(String id) {
        SysRoleVO vo = new SysRoleVO();
        SysRole sysRole = getById(id);
        if (sysRole != null) {
            BeanUtils.copyProperties(sysRole, vo);
            vo.setMenuIds(sysRoleMenuService.getRoleMenuLeafIds(id));
            return vo;
        }
        return null;
    }
}




